package license_key

import (
	"context"
	v1 "meta-self/api/client/license_key/v1"
	"meta-self/internal/service"
)

// CalcPrice 计算卡密价格
func (c *ControllerV1) CalcPrice(ctx context.Context, req *v1.CalcPriceReq) (res *v1.CalcPriceRes, err error) {
	result, err := service.LicenseKey().CalcPrice(ctx, &req.LicenseKeyCalcPriceInp)
	if err != nil {
		return nil, err
	}
	res = &v1.CalcPriceRes{LicenseKeyCalcPriceModel: result}
	return
}

// CreateOrder 创建卡密订单
func (c *ControllerV1) CreateOrder(ctx context.Context, req *v1.CreateOrderReq) (res *v1.CreateOrderRes, err error) {
	result, err := service.LicenseKey().CreateOrder(ctx, &req.LicenseKeyCreateOrderInp)
	if err != nil {
		return nil, err
	}
	res = &v1.CreateOrderRes{LicenseKeyCreateOrderModel: result}
	return
}

// View 获取卡密详情
func (c *ControllerV1) View(ctx context.Context, req *v1.ViewReq) (res *v1.ViewRes, err error) {
	result, err := service.LicenseKey().View(ctx, &req.LicenseKeyViewInp)
	if err != nil {
		return nil, err
	}
	res = &v1.ViewRes{LicenseKeyViewModel: result}
	return
}

// List 获取卡密列表
func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListRes, err error) {
	list, total, err := service.LicenseKey().List(ctx, &req.LicenseKeyListInp)
	if err != nil {
		return nil, err
	}
	res = &v1.ListRes{
		List: list,
	}
	res.PageRes.Pack(&req.PageReq, total)
	return
}

// HistoryList 获取卡密历史记录列表
func (c *ControllerV1) HistoryList(ctx context.Context, req *v1.HistoryListReq) (res *v1.HistoryListRes, err error) {
	list, total, err := service.LicenseKey().HistoryList(ctx, &req.LicenseKeyHistoryListInp)
	if err != nil {
		return nil, err
	}
	res = &v1.HistoryListRes{
		List: list,
	}
	res.PageRes.Pack(&req.PageReq, total)
	return
}

// ApplyRefund 申请卡密退款
func (c *ControllerV1) ApplyRefund(ctx context.Context, req *v1.ApplyRefundReq) (res *v1.ApplyRefundRes, err error) {
	result, err := service.LicenseKey().ApplyRefund(ctx, &req.LicenseKeyApplyRefundInp)
	if err != nil {
		return nil, err
	}
	res = &v1.ApplyRefundRes{LicenseKeyApplyRefundModel: result}
	return
}
